package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * <新闻 - 千龙网> 解析类
 *
 * @author KK
 *
 */
public class QianlongNewsAnalyse implements AnalyseNews {



	
	public boolean isDetailPage(String url) {
		String[] regex={
				"http://[a-z]*.qianlong.com/[0-9]*/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]*@[0-9]*.htm[l]?",
				"http://[a-z]*.qianlong.com/[a-z]*/[a-z]*/[a-z]*/[0-9]*.htm[l]?",
				"http://[a-z]*.qianlong.com/[a-z]*/[a-z]*/[0-9]{4}/[0-9]{4}/[0-9]*.html[l]?",
				"http://[a-z]*.qianlong.com/[0-9]*/[0-9]*/[0-9]*/[0-9]*/[0-9]*%[0-9]*.htm"

				};

		return RegexUtils.matchAny(url,regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		String title=null;
		String content=null;
		Long date=null;
		String dateStr=null;
		String commnetNum=null;
		String clickNum=null;
		Document doc=Jsoup.parse(htmltxt);
		String author=null;
		title=doc.select("div#content h1").text();
		if(StringUtils.isNotBlank(title)){
			author=doc.select("#source a").text();
			dateStr=doc.select("span#Date").text();
			 Elements select=doc.select("div#content");
			 select.select("#urlAndDate,h1").remove();
			 content=HtmlCleaner.getContentHtml(url,select);
			date=DateUtils.matchDate(dateStr);
		}else{
			title=doc.select("div#con_border h1").text();
			if(StringUtils.isNotBlank(title)){
				dateStr=doc.select("cite.info_text").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("div#zbContent"));
				date=DateUtils.matchDate(dateStr);
				author=StringUtils.substringAfter(dateStr, "来源：");
			}
		}
		
		if(StringUtils.isBlank(title)){
			title=doc.select(".title").text();
		}
		if(StringUtils.isBlank(content)){
			content=doc.select("#content").html();
		}
		if(date==null){
			date=DateUtils.matchDate(doc.select(".pubDate").text());
		}
			NewsMeta news=new NewsMeta();
			news.setUrl(url);
			news.setType(1);
			news.setTitle(StringUtils.trimSpace(title));
			news.setCommentNum(NumberUtils.parseInt(commnetNum));
			news.setClickNum(NumberUtils.parseInt(clickNum));
			news.setContent(StringUtils.trimSpace(content));
			news.setDate(date);
			news.setAuthor(author);
			return news;
	}

	public static void main(String[] args) {
		QianlongNewsAnalyse a=new QianlongNewsAnalyse();
		String url="http://tech.qianlong.com/33443/2014/03/21/6484@9490244.htm";
		System.out.println(a.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
